package com.example.news_reader;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ExpandableListView;
import android.widget.ProgressBar;
import android.widget.ExpandableListView.OnChildClickListener;

public class WebLink extends Activity{

	/** Called when the activity is first created. */

	WebView wv;
	ProgressBar pb;
	String url = new String();
	WebChromeClient wcc;
	String query = null;
//	private final int WHAT = 0;
//	
//	Handler handler = new Handler(){
//		public void handleMessage(Message msg){
//			switch (msg.what){
//				case WHAT:
//					t.stop();
//					break;
//			}
//		}
//	};
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(Menu.NONE, Menu.FIRST+1, Menu.NONE, "웹페이지 보기");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		boolean a = false;

		switch(item.getItemId()){
			case Menu.FIRST+1:
				wv.setWebChromeClient(wcc);
				t.run();
			a=true;
			break;
		}

		return (a || super.onOptionsItemSelected(item));
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webview);

		Intent i = getIntent();
		Bundle extra = i.getExtras();
		if(extra!=null){
			url = extra.getString("link");
		}

		wv = (WebView)findViewById(R.id.wv);
		pb = (ProgressBar)findViewById(R.id.pb);
		pb.setVisibility(View.VISIBLE);
		wcc = new WebChromeClient(){

			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				// TODO Auto-generated method stub
				super.onProgressChanged(view, newProgress);
				if(newProgress == pb.getMax())
				{
					WebLink.this.setTitle(R.string.app_name);
					WebLink.this.setProgress(0);
					pb.setVisibility(View.GONE);
				}
				else
				{
					pb.setVisibility(View.VISIBLE);
					WebLink.this.setTitle("Loading...");
					WebLink.this.setProgress(newProgress*100);
					pb.setProgress(newProgress);
				}
			}
		};

		wv.setWebChromeClient(wcc);
		t.start();
	}

	Thread t = new Thread(
			new Runnable() {
				public void run() {
					wv.setWebViewClient(new WebViewClient(){
						@Override
						public void onPageStarted(WebView view, String url, Bitmap favicon) {
							// TODO Auto-generated method stub
							super.onPageStarted(view, url, favicon);
						}

						@Override
						public void onPageFinished(WebView view, String url) {
							// TODO Auto-generated method stub
							super.onPageFinished(view, url);
							pb.setProgress(100);
							pb.setVisibility(View.GONE);
						}
					});
					WebSettings set = wv.getSettings();
					set.setBuiltInZoomControls(true);

						try {
							if(query==null){
								query = "http://www.google.co.kr/gwt/x?u="+
										URLEncoder.encode(url, "utf-8")+
										"&btnGo=%EC%A0%9C%EC%B6%9C&source=wax&ie=UTF-8&oe=UTF-8";
							}else {
								query = url;
							}
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					
					wv.loadUrl(query);
//					Message msg = handler.obtainMessage(WHAT);
//					handler.sendMessage(msg);
				}
			}
			);



}
